﻿@using KelonProud.Mvc.Models
@{
    Layout = "~/Views/Shared/_QueryMaster.cshtml";
}

@section styles
    {
}
@section scripts{



    <script>
        $(function() {

            //abp.services.admin.navigation.getNavigationTreeJson().done(function(aaa) {
            //    var aa = aaa;
            //});

            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();

            //2.初始化Button的点击事件
            var oButtonInit = new ButtonInit();
            oButtonInit.Init();

            $(window).resize(function() {
                $('#tb_departments').bootstrapTable('resetView',
                    {
                        height: getTableHeight()
                    });
            });
        });

        var TableInit = function() {

            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function() {
                $('#tb_departments').bootstrapTable({
                    url: '/api/services/admin/navigation/GetNavigations', //请求后台的URL（*）
                    method: 'post', //请求方式（*）
                    toolbar: '#toolbar', //工具按钮用哪个容器
                    striped: true, //是否显示行间隔色
                    cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true, //是否显示分页（*）
                    sortable: false, //是否启用排序
                    sortOrder: "asc", //排序方式
                    queryParams: oTableInit.queryParams, //传递参数（*）
                    sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1, //初始化加载第一页，默认第一页
                    pageSize: 20, //每页的记录行数（*）
                    pageList: [20, 25, 50, 100], //可供选择的每页的行数（*）
                    search: false, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                    strictSearch: true,
                    showColumns: true, //是否显示所有的列
                    showRefresh: true, //是否显示刷新按钮
                    minimumCountColumns: 2, //最少允许的列数
                    clickToSelect: true, //是否启用点击选中行
                    height: getTableHeight(), //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId: "id", //每一行的唯一标识，一般为主键列
                    showToggle: false, //是否显示详细视图和列表视图的切换按钮
                    cardView: false, //是否显示详细视图
                    detailView: false, //是否显示父子表
                    columns: [
                        {
                            checkbox: true
                        }, {
                            field: 'name',
                            title: '菜单名称'
                        }, {
                            field: 'code',
                            title: '菜单编号'
                        }, {
                            field: 'namePath',
                            title: '菜单路径'
                        }, {
                            field: 'actionUrl',
                            title: '请求地址'
                        }, {
                            field: 'typeName',
                            title: '类型'
                        }, {
                            field: 'icon',
                            title: '图标'
                        }, {
                            field: 'level',
                            title: '级别',
                            align: 'right'
                        }, {
                            field: 'order',
                            title: '顺序',
                            align: 'right'
                        }, {
                            field: 'isHide',
                            title: '是否隐藏',
                            wdith: 50,
                            align: 'center',
                            formatter: function(value, row, index) {
                                if (value) {
                                    return '<input type="checkbox" checked disabled />';
                                } else {
                                    return '<input type="checkbox" disabled />';
                                }
                            }
                        }, {
                            field: 'description',
                            wdith: 500,
                            title: '描述'
                        }
                    ]
                });
            };

            //得到查询的参数
            oTableInit.queryParams = function(params) {
                var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    SearchCount: params.limit, //页面大小
                    SkipCount: params.offset, //页码
                    Name: $("#txt_search_name").val(),
                    Code: $("#txt_search_code").val(),
                    Type: $("#Type").val()
            };
                return temp;
            };
            return oTableInit;
        };

        var ButtonInit = function() {
            var oInit = new Object();
            var postdata = {};

            oInit.Init = function() {
                //初始化页面上面的按钮事件

                $("#btn_add").click(function() {
                    DiyWindow.modal({
                        id: "insertNav",
                        title: '新增菜单',
                        width: 850,
                        height: 670,
                        showMaxBtn: false,
                        url: '@Url.Action("Create", "Navigation")',
                        onClose: onRefresh
                    });
                });

                $("#btn_edit").click(function() {
                    var row = getSelectRows();

                    if (row != null) {
                        if (row.length !== 1) {
                            abp.notify.error('一次只能编辑一条数据。');
                        } else {
                            DiyWindow.modal({
                                id: "insertNav",
                                title: '编辑菜单',
                                width: 850,
                                height: 670,
                                showMaxBtn: false,
                                url: '@Url.Action("Edit", "Navigation")?id=' + row[0].id,
                                onClose: onRefresh
                            });
                        }
                    }
                });

                $("#btn_delete").click(function() {
                    var rows = getSelectRows();

                    if (rows != null) {
                        abp.message.confirm("您确认删除这些菜单吗？",
                            "确认删除菜单",
                            function(isConfrim) {
                                if (isConfrim) {
                                    var ids = [];

                                    $(rows).each(function() {
                                        ids.push(this.id);
                                    });

                                    abp.services.admin.navigation.deleteMenuByIds(ids.join(',')).done(function() {
                                        abp.notify.success("菜单删除成功!");
                                        onRefresh();
                                    });
                                }
                            });
                    }
                });

                $("#btn_query").click(function() {
                    onRefresh();
                });
            };

            return oInit;
        };

        function onRefresh() {
            $('#tb_departments').bootstrapTable('refresh');
        }

        function getTableHeight() {
            return $(".body-content").height() - $("#searchPanel").height() - 40;
        }

        function getSelectRows() {
            var result = $('#tb_departments').bootstrapTable('getSelections');

            if (result.length === 0) {
                abp.notify.error('请选择一行数据。');

                return null;
            }

            return result;
        }

    </script>
}
<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default" id="searchPanel">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-1" for="txt_search_name">菜单名称</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="txt_search_name">
                    </div>
                    <label class="control-label col-sm-1" for="txt_search_code">菜单编码</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="txt_search_code">
                    </div>
                    <label class="control-label col-sm-1" for="txt_search_code">菜单类型</label>
                    <div class="col-sm-2" style="text-align:left;">
                        @Html.Partial("Modals/Components/_DropDownSelectView", new DropDownSelectModel()
                   {
                       Name = "Type",
                       Url = "/api/services/admin/navigation/GetNavigationTypes",
                       IdKey = "id",
                       TextKey = "name",
                       AllowSearch = false,
                       AllowNull = true,
                       NullItemText = "全部",
                       NullItemValue = "-1",
                       Placeholder = "全部"
                   })
                    </div>
                    <div class="col-sm-1" style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-primary"><i class="fa fa-search"></i> 查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <button action data-action-code='S0001_ADD' id="btn_add" type="button" class="btn btn-success">
            <span class="fa fa-plus" aria-hidden="true"></span> 新增
        </button>
        <button action data-action-code='S0001_EDIT' id="btn_edit" type="button" class="btn btn-primary">
            <span class="fa fa-pencil" aria-hidden="true"></span> 修改
        </button>
        <button action data-action-code='S0001_DELETE' id="btn_delete" type="button" class="btn btn-danger">
            <span class="fa fa-remove" aria-hidden="true"></span> 删除
        </button>
    </div>
    <table id="tb_departments"></table>
</div>